package com.zjhn.ds.task;

import com.alibaba.fastjson.JSON;
import com.zjhn.ds.common.Constant;
import com.zjhn.ds.common.annotate.IgnoreTenant;
import com.zjhn.ds.netty.ChannelGroups;
import com.zjhn.ds.utils.base.RedisCache;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @BelongsProject: ds_jlw
 * @BelongsPackage: com.zjhn.ds.task
 * @Author: sxt
 * @CreateTime: 2023-12-21  15:59
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@Configuration
public class WebStationStatusTask {

    @Resource
    private RedisCache redisCache;

    @IgnoreTenant
    @Scheduled(fixedDelay = 1000 * 10)
    public void statics() {
        List<String> list = new ArrayList<>();
        Set<String> keys = redisCache.keys(Constant.REDIS_PRE.STATION_STATUS + "*");
        for (String key : keys) {
            Object status = redisCache.getCacheObject(key);
            if (status == null) status = "0";
            list.add(key.split(":")[1] + "|" + status);
        }
        if (ChannelGroups.size() > 0)
            ChannelGroups.broadcast(new TextWebSocketFrame(JSON.toJSONString(list)));
    }
}
